package com.adou.p2p.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.adou.p2p.base.controller.AbstractBaseApiController;
import com.adou.p2p.base.controller.NotFoundException;
import com.adou.p2p.model.ProductValue;
import com.adou.p2p.page.ErrorCode;
import com.adou.p2p.page.PageResult;
import com.adou.p2p.page.ResponseVo;
import com.adou.p2p.service.ProductService;

@RestController
@RequestMapping("product")
@Api(tags = "product")
public class ProductController extends AbstractBaseApiController {
	@Autowired
	private ProductService productService;

	@ApiOperation("新增")
	@PostMapping(value = "createProduct")
	public void createProduct(HttpServletRequest request, HttpServletResponse response) {
		ProductValue productValue = new ProductValue();
		try {

			BeanUtils.populate(productValue, request.getParameterMap());
			productService.createProduct(productValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
//	@RequestMapping(value = "findAll")
//	public ResponseEntity<List<ProductValue>> findAll() {
//		List<ProductValue> values = productService.findAll();
//		return new ResponseEntity<List<ProductValue>>(values, HttpStatus.OK);
//	}
	
	@ApiOperation("根据id查询")
	@PostMapping(value = "findById")
	public ResponseEntity<ProductValue> findById(@RequestParam("id") long id) {
		try {
			ProductValue branch = productService.findById(id);
			return new ResponseEntity<>(branch, HttpStatus.OK);
		} catch (Exception e) {
			throw new NotFoundException(e.getMessage());
		}
	}

	@ApiOperation("更新")
	@PostMapping(value = "updateProduct")
	public void updateProduct(HttpServletRequest request, HttpServletResponse response) {
		ProductValue productValue = new ProductValue();
		try {

			BeanUtils.populate(productValue, request.getParameterMap());
			productService.updateProduct(productValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	@ApiOperation("分页查询")
	@PostMapping("page")
	public ResponseVo<String> qryProductPage(@RequestParam(required = false) String proName,
			@RequestParam(required = false, defaultValue = "1") Integer pageNumber,
			@RequestParam(required = false, defaultValue = "2") Integer pageSize) {

		ProductValue dto = ProductValue.builder().proName(proName).build();

		PageResult<ProductValue> result = productService.qryProductValuePage(pageNumber, pageSize, null, null, dto);
		ResponseVo.Page pageVo = new ResponseVo.Page(pageNumber, pageSize, (int) result.getTotalElements());

		return result.getContent() != null ? ResponseVo.success(result.getContent(), pageVo)
				: ResponseVo.failtrue(ErrorCode.SELECT_ERROR, "我发获取产品信息");

	}

}
